//
//  faveriterCard.m
//  QRMaster
//
//  Created by myApple on 15/7/15.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "faveriterCard.h"
#import "OpertionPlist.h"
#import "RequestNetWork.h"
#import "PromptInfoView.h"

@implementation faveriterCard

+(void)faveriterOrCancelCardByRoomId:(NSMutableDictionary *)cardInfo result:(cardResult )block
{
    NSMutableDictionary * dict = [OpertionPlist readDataFromPlist];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    __block NSMutableArray * cardArray = [dict objectForKey:FAVERITCARD];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[cardInfo objectForKey:@"communityId"] forKey:@"communityId"];
    [postDict setValue:[userInfo objectForKey:@"uid"]forKey:@"id"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [postDict setValue:[cardInfo objectForKey:@"roomId"] forKey:@"roomId"];
    //添加收藏
    if (cardArray.count == 0) {
        RequestNetWork * rn = [RequestNetWork sharedRequestNetWork];
        [rn initWithPath:POST(ADDCOLLECTROOM) params:postDict block:^(id downLoadData, id error) {
            if (downLoadData) {
                cardArray = [[NSMutableArray alloc]init];
                [cardArray addObject:cardInfo];
                [dict setValue:cardArray forKey:FAVERITCARD];
                [OpertionPlist writeDataToPlist:dict];
                if (block) {
                    block(KCSUCCESS_FAVERITER);
                }
            }
            else
            {
                [PromptInfoView showMessage:[error objectForKey:@"info"]];
            }
        }];
    }
    else
    {
         //判断本地是否存在，存在就删除，不存在就添加
            __block NSDictionary * newDict = nil;
            NSString * roomid = [cardInfo objectForKey:@"roomId"];
            for (NSDictionary * cardDict in cardArray) {
                if ([roomid isEqualToString:[cardDict objectForKey:@"roomId"]]) {
                    newDict = cardDict;
                }
                
            }
            if (newDict == nil ||[newDict isEqual:[NSNull null]]) {
                if (cardArray.count > 4) {
                    if (block) {
                        block(KCFAIL_FAVERITER);
                    }
                    return;
                }
                RequestNetWork * rn = [RequestNetWork sharedRequestNetWork];
                [rn initWithPath:POST(ADDCOLLECTROOM) params:postDict block:^(id downLoadData, id error) {
                    if (downLoadData) {
                        [cardArray addObject:cardInfo];
                        [dict setValue:cardArray forKey:FAVERITCARD];
                        [OpertionPlist writeDataToPlist:dict];
                        if (block) {
                            block(KCSUCCESS_FAVERITER);
                        }
                    }
                    else
                    {
                        [PromptInfoView showMessage:[error objectForKey:@"info"]];
                    }
                }];
            }
            else
            {
                RequestNetWork * rn = [RequestNetWork sharedRequestNetWork];
                [rn initWithPath:POST(DELETECOLLECTROOM) params:postDict block:^(id downLoadData, id error) {
                    if (downLoadData) {
                        [cardArray removeObject:newDict];
                        [dict setValue:cardArray forKey:FAVERITCARD];
                        [OpertionPlist writeDataToPlist:dict];
                        if (block) {
                            block(KCCANCEL_FAVERITER);
                        }
                    }
                    else
                    {
                        [PromptInfoView showMessage:[error objectForKey:@"info"]];
                    }
                }];
            }
        
    }
    
}

//获取网络收藏房卡
+(void)writeFaveriterCard:(NSArray *)array
{
    NSMutableDictionary * dict = [OpertionPlist readDataFromPlist];
    if (array.count > 0) {
        [dict setValue:array forKey:FAVERITCARD];
        [OpertionPlist writeDataToPlist:dict];
    }
}

@end
